/*
 * CHttpService.h
 *
 *  Created on: 2018年1月8日
 *      Author: terry
 */

#ifndef CHTTPSERVICE_H_
#define CHTTPSERVICE_H_

#include "json/json.h"
#include "SharedPtr.h"
#include "RestHttpHandler.h"
#include "DataStore.h"
#include <map>

typedef std::map< std::string, std::string >		StringMap;

class CHttpService: public RestHttpHandler
{
public:
	CHttpService();
	virtual ~CHttpService();

	void setDb(std::shared_ptr< DataStore >& dbStore);

	bool addStatic(const std::string& filedir, const std::string& urlPath);

    virtual bool start(int port, const std::string& webroot);

    virtual void stop();

    virtual bool isStarted();

    virtual void broadcast(void *data, size_t len);

    virtual void broadcast(const std::string& data);


    static std::string getWebsitePath();
    static std::string getUserPath();
    static std::string getUploadPath();

    virtual void handleUpload(const char* name, const std::string& filepath);

    virtual int handleFileApi(HttpRequest& request, std::string& resp, std::string& mime);

    virtual void handleWebSocketFrame(unsigned char *data, size_t size, unsigned char flags);

protected:
    void setupUri();

protected:
    int searchUser(HttpRequest& request, std::string& jsonText);
    int addUser(HttpRequest& request, std::string& jsonText);
    int delUser(HttpRequest& request, std::string& jsonText);
    int updateUser(HttpRequest& request, std::string& jsonText);
    int getUser(HttpRequest& request, std::string& jsonText);
    int enableUser(HttpRequest& request, std::string& jsonText);

    int existUserName(HttpRequest& request, std::string& jsonText);

    int searchEvent(HttpRequest& request, std::string& jsonText);
    int delEvent(HttpRequest& request, std::string& jsonText);
    int getEvent(HttpRequest& request, std::string& jsonText);

    int searchImage(HttpRequest& request, std::string& jsonText);
	int delImage(HttpRequest& request, std::string& jsonText);
	int getImage(HttpRequest& request, std::string& jsonText);

    int setPassword(HttpRequest& request, std::string& jsonText);
	int changePassword(HttpRequest& request, std::string& jsonText);

protected:
	int reboot(HttpRequest& request, std::string& jsonText);
	int reset(HttpRequest& request, std::string& jsonText);
	int upgrade(HttpRequest& request, std::string& jsonText);

	int getVersion(HttpRequest& request, std::string& jsonText);

	bool parseJson(const mg_str& str, Json::Value& json);

	std::string makeUrl(const std::string& filepath, const std::string& dir, const std::string& webroot);
	std::string getHostIp();

	void addUrl(JsonList& jsonList);
	void addUrl(Json::Value& json);

protected:
	bool hasUser(const std::string& id);
	bool hasUserName(const std::string& username);
	bool queryUserName(const std::string& id, std::string& username);

	bool getUrlPathMap(const std::string& filepath, std::string& filedir, std::string& urlPath);

	bool matchDir(const std::string& filepath, const std::string& filedir);

protected:
    std::shared_ptr<HttpServer>     m_server;
	std::string	m_uploadFilePath;
	int	m_port;

	std::shared_ptr< DataStore >	m_dbStore;

	StringMap	m_staticMap;

};

#endif /* CHTTPSERVICE_H_ */
